#include "template.hpp"
// using namespace std;

void fun_string()
{
    // 1. String initialization
    string s1;           // Empty string
    string s2 = "Hello"; // Initialize with a literal
    string s3(5, 'a');   // "aaaaa"
    string s4 = s2;      // Copy s2
    string s5(s2, 1, 3); // Substring from s2[1], length 3 -> "ell"

    cout << "Strings: " << s2 << " " << s3 << " " << s5 << endl;

    // 2. Basic properties
    cout << "Size: " << s2.size() << endl;
    cout << "Is empty: " << s1.empty() << endl;
    cout << "Access: s2[1] = " << s2[1] << ", at(1) = " << s2.at(1) << endl;

    // 3. Modify string
    s2.push_back('!');   // Add character at the end
    s2.append(" World"); // Append string
    cout << "After append: " << s2 << endl;

    s2.insert(5, " C++"); // Insert at index 5
    cout << "After insert: " << s2 << endl;

    s2.erase(5, 4); // Remove 4 characters from index 5
    cout << "After erase: " << s2 << endl;

    s2.replace(6, 5, "STL"); // Replace 5 characters from index 6 with "STL"
    cout << "After replace: " << s2 << endl;

    // 4. Find substring
    size_t pos = s2.find("STL");
    if (pos != string::npos)
    {
        cout << "Found STL at index: " << pos << endl;
    }

    // 5. Substring
    string sub = s2.substr(0, 5); // Substring from index 0, length 5
    cout << "Substring: " << sub << endl;

    // 6. Compare strings
    string a = "abc", b = "abd";
    cout << "Compare abc vs abd: " << (a < b ? "abc < abd" : "abd < abc") << endl;

    // 7. String to number and back
    string num = "12345";
    int x = stoi(num);
    cout << "stoi conversion: " << x << endl;
    string strNum = to_string(6789);
    cout << "to_string conversion: " << strNum << endl;

    // 8. Iterate over string
    cout << "Loop 1: ";
    for (int i = 0; i < (int)s2.size(); i++)
        cout << s2[i];
    cout << endl;

    cout << "Loop 2: ";
    for (auto ch : s2)
        cout << ch;
    cout << endl;

    cout << "Loop 3: ";
    for (auto it = s2.begin(); it != s2.end(); ++it)
        cout << *it;
    cout << endl;
}

// int main() {
//     fun_string();
//     return 0;
// }
